package com.secneo.appshare.activities;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Medals;
import com.secneo.appshare.api.impl.RemoteApiImpl;

public class MemberSettingHonorActivity extends Activity {
	public TextView memberSettingHonorNumberTextView,
			memberSettingHonorGetOneTextView, memberSettingHonorGetTwoTextView;
	public String oneName = "", twoName = "";
	public boolean oneHasit, twoHasit;
	public static final int GETMEDALS = 2020;
	int honorNumber = 0;
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GETMEDALS:
				System.out.println("dffffffffffffffffffffffffffffffffffffffff");
				if (oneHasit) {
					honorNumber++;
					memberSettingHonorGetOneTextView
							.setText(MemberSettingHonorActivity.this
									.getResources()
									.getString(
											R.string.member_setting_honor_gethonor));
				} else {
					memberSettingHonorGetOneTextView
							.setText(MemberSettingHonorActivity.this
									.getResources()
									.getString(
											R.string.member_setting_honor_not_gethonor,
											oneName));
				}
				if (twoHasit) {
					honorNumber++;
					memberSettingHonorGetTwoTextView
							.setText(MemberSettingHonorActivity.this
									.getResources()
									.getString(
											R.string.member_setting_honor_gethonor));
				} else {
					memberSettingHonorGetTwoTextView
							.setText(MemberSettingHonorActivity.this
									.getResources()
									.getString(
											R.string.member_setting_honor_not_gethonor,
											twoName));
				}

				memberSettingHonorNumberTextView
						.setText(MemberSettingHonorActivity.this.getResources()
								.getString(
										R.string.member_setting_honor_number,
										honorNumber));

				break;
			default:
				break;

			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.member_setting_honor);
		AppShareApplication.getInstance().addActivity(this);
		LocalStore.getLocaleStoreInstance(this).setHonorCounts(0) ;
		memberSettingHonorNumberTextView = (TextView) findViewById(R.id.MemberSettingHonorNumberTextView);
		memberSettingHonorGetOneTextView = (TextView) findViewById(R.id.MemberSettingHonorGetOneTextView);
		memberSettingHonorGetTwoTextView = (TextView) findViewById(R.id.MemberSettingHonorGetTwoTextView);
		
		Button retBtn = (Button) findViewById(R.id.img_ret);
		retBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		ImageView homeBtn = (ImageView) findViewById(R.id.img_home);
		homeBtn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent=new Intent();
				intent.setClass(MemberSettingHonorActivity.this, MainActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				finish();
			}
		});
		Thread honorThread = new Thread() {
			public void run() {
				String uId = LocalStore.getLocaleStoreInstance(
						MemberSettingHonorActivity.this).getUid();
				RemoteApiImpl rai = new RemoteApiImpl();
				List<Medals> ret = rai.getMedals(
						MemberSettingHonorActivity.this, uId);
				if (ret != null) {
					if (ret.size() == 2) {
						Medals m = ret.get(0);
						oneName = m.name;
						oneHasit = m.hasit;
						Medals m1 = ret.get(1);
						twoName = m1.name;
						twoHasit = m1.hasit;
					}
					Message msg = new Message();
					msg.what = GETMEDALS;
					mHandler.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = GETMEDALS;
					mHandler.sendMessage(msg);
				}
			}
		};
		honorThread.start();

	}
}
